package com.boxin.ims.modules.wechat.busi;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.momarketing.common.QRCodeUtils;
import com.boxin.ims.modules.wechat.entity.NewsReply;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.WechatDaoHelper;

/**
 * @author Jakemanse
 * @date 2013-10-23
 * @function <p> 查询我的抽奖，用户自己的抽奖 </p>
 */
@Service
public class LotteryQueryProcessor extends BusinessProcessor {
	
	

	@Autowired
	private WechatDaoHelper wechatDaoHelper;
	
	public LotteryQueryProcessor() {
		super();
	}

	@Override
	public ProcessMessage process() {
		WechatMember member =  memberCheckAndReply();
		queryMineLottery( member );
		return null;
	}

	@Override
	public boolean isNeedBeMember() {
		// TODO Auto-generated method stub
		return false;
	}
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-18  下午9:32:25
	 * @function <p> 查询我的中奖记录  </p>
	 */
	public void queryMineLottery(WechatMember member ){
		//看是否有未消费的奖品如果有则不允许再抽取
		String reply ="亲,对不起,未找到您的获奖信息,谢谢您的参与!";
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("userId", user.getId()+"");
		params.put("memberId", member.getId()+"");
		List<Map<String, Object> > myLtyLst = wechatDaoHelper.queryMineLotteryRecordByMember(params);	//主动抽奖
		List<Map<String, Object> > myLtyLst2 = wechatDaoHelper.queryMyRegLotteryRecordByMember(params);	//主动抽奖
		List<Map<String, Object> > myLtyLst3 = wechatDaoHelper.queryMyAdvertLotteryRecordByMember(params);	//广告奖品
		
		List<Map<String, Object> >  lotteryList = new ArrayList<Map<String, Object> >();
		
		lotteryList.addAll(myLtyLst);
		lotteryList.addAll(myLtyLst2);
		lotteryList.addAll(myLtyLst3);
		
		
		if(lotteryList != null && !lotteryList.isEmpty()){
			
			
			if(lotteryList.size()==1){	//单条奖品
				Map map =  lotteryList.get(0);
				Date date = (Date) map.get("end_time");
				String validDate = DateUtils.formatDate(
						(date == null) ? new Date() : date, "MM月dd日");
				
				String title = "您有未使用的奖品【" + map.get("name") + "】";
				String desc = "亲，您有一份未使用的奖品【" + map.get("name") + "】,请于"
						+ validDate + "之前到店里凭此二维码核销使用，过期作废，如果您想放弃本奖品请输入【放弃"+map.get("id")+"】，谢谢！";
				writeSingleLotteryImageToUser(title, desc,
						Long.parseLong(map.get("qrcode_id").toString()));
				return ;
				
			}else{//多条奖品
				List<NewsReply> newsList = new ArrayList<NewsReply>();
				for (Map<String, Object> row : lotteryList) {
					NewsReply newsReply = new NewsReply();
					
					Date date = (Date) row.get("end_time");
					String validDate = DateUtils.formatDate(
							(date == null) ? new Date() : date, "MM月dd日");
					
					newsReply.setTitle("您有未使用的奖品【"+row.get("name").toString()+"】");
					newsReply.setDescription("您有未使用的奖品【"
							+ row.get("name").toString()+"】请于"+validDate+"之前到店里凭此打开的二维码到服务台核销使用,如果您想放弃本奖品请输入【放弃"+row.get("id")+"】，谢谢!");
					newsReply.setPicUrl(QRCodeUtils.getQRCodePNGURL(Long
							.parseLong(row.get("qrcode_id").toString())));
					newsReply.setUrl(QRCodeUtils.getQRCodePNGURL(Long
							.parseLong(row.get("qrcode_id").toString())));
					newsList.add(newsReply);
				}

				writeQueryLotteryImageToUser(newsList); // 回复
				return ;
				
			}
			
		}		
		
		writeToUser(reply);	//未找到奖品信息
	}
	
	
	
	

}
